D:\git\skunkworks\herald-for-cpp\herald\include\herald\datatype\uuid.h
Line | Count | Source |
1 | | // Copyright 2020-2021 Herald Project Contributors |
2 | | // SPDX-License-Identifier: Apache-2.0 |
3 | | // |
4 | | |
5 | | #ifndef HERALD_UUID_H |
6 | | #define HERALD_UUID_H |
7 | | |
8 | | #include "error_code.h" |
9 | | #include "randomness.h" |
10 | | |
11 | | #include <string> |
12 | | #include <array> |
13 | | |
14 | | namespace herald { |
15 | | namespace datatype { |
16 | | |
17 | | // High level UUID class template |
18 | | class UUID { |
19 | | public: |
20 | | using value_type = uint8_t; |
21 | | |
22 | | static UUID fromString(const std::string& from) noexcept; |
23 | | // static UUID fromString(const char* from) noexcept; |
24 | | |
25 | | template <typename RandomnessSourceT> |
26 | 5 | static UUID random(RandomnessGenerator<RandomnessSourceT>& from) noexcept { |
27 | 5 | std::array<value_type, 16> data{ {0} }; |
28 | 5 | Data randomness; |
29 | 5 | from.nextBytes(16,randomness); |
30 | 85 | for (std::size_t i = 0;i < 16;i++80 ) { |
31 | 80 | data[i] = (value_type)randomness.at(i); |
32 | 80 | } |
33 | 5 | // Now set bits for v4 UUID explicitly |
34 | 5 | constexpr value_type M = 0x40; // 7th byte = 0100 in binary for MSB 0000 for LSB - v4 UUID |
35 | 5 | constexpr value_type N = 0x80; // 9th byte = 1000 in binary for MSB 0000 for LSB - variant 1 |
36 | 5 | data[6] = (0x0f & data[6]) | M; // blanks out first 4 bits |
37 | 5 | data[8] = (0x3f & data[8]) | N; // blanks out first 2 bits |
38 | 5 | UUID uuid(data,false); // TODO generate random data and tag as v4 |
39 | 5 | return uuid; // returns copy |
40 | 5 | } ??$random@VAllZerosNotRandom@datatype@herald@@@UUID@datatype@herald@@SA?AV012@AEAV?$RandomnessGenerator@VAllZerosNotRandom@datatype@herald@@@12@@Z Line | Count | Source | 26 | 1 | static UUID random(RandomnessGenerator<RandomnessSourceT>& from) noexcept { | 27 | 1 | std::array<value_type, 16> data{ {0} }; | 28 | 1 | Data randomness; | 29 | 1 | from.nextBytes(16,randomness); | 30 | 17 | for (std::size_t i = 0;i < 16;i++16 ) { | 31 | 16 | data[i] = (value_type)randomness.at(i); | 32 | 16 | } | 33 | 1 | // Now set bits for v4 UUID explicitly | 34 | 1 | constexpr value_type M = 0x40; // 7th byte = 0100 in binary for MSB 0000 for LSB - v4 UUID | 35 | 1 | constexpr value_type N = 0x80; // 9th byte = 1000 in binary for MSB 0000 for LSB - variant 1 | 36 | 1 | data[6] = (0x0f & data[6]) | M; // blanks out first 4 bits | 37 | 1 | data[8] = (0x3f & data[8]) | N; // blanks out first 2 bits | 38 | 1 | UUID uuid(data,false); // TODO generate random data and tag as v4 | 39 | 1 | return uuid; // returns copy | 40 | 1 | } |
??$random@VIntegerDistributedRandomSource@datatype@herald@@@UUID@datatype@herald@@SA?AV012@AEAV?$RandomnessGenerator@VIntegerDistributedRandomSource@datatype@herald@@@12@@Z Line | Count | Source | 26 | 4 | static UUID random(RandomnessGenerator<RandomnessSourceT>& from) noexcept { | 27 | 4 | std::array<value_type, 16> data{ {0} }; | 28 | 4 | Data randomness; | 29 | 4 | from.nextBytes(16,randomness); | 30 | 68 | for (std::size_t i = 0;i < 16;i++64 ) { | 31 | 64 | data[i] = (value_type)randomness.at(i); | 32 | 64 | } | 33 | 4 | // Now set bits for v4 UUID explicitly | 34 | 4 | constexpr value_type M = 0x40; // 7th byte = 0100 in binary for MSB 0000 for LSB - v4 UUID | 35 | 4 | constexpr value_type N = 0x80; // 9th byte = 1000 in binary for MSB 0000 for LSB - variant 1 | 36 | 4 | data[6] = (0x0f & data[6]) | M; // blanks out first 4 bits | 37 | 4 | data[8] = (0x3f & data[8]) | N; // blanks out first 2 bits | 38 | 4 | UUID uuid(data,false); // TODO generate random data and tag as v4 | 39 | 4 | return uuid; // returns copy | 40 | 4 | } |
|
41 | | |
42 | | UUID(const char* from) noexcept; |
43 | | UUID(UUID&& from) noexcept; |
44 | | UUID(const UUID& from) noexcept; |
45 | | ~UUID() noexcept = default; |
46 | | |
47 | | UUID& operator=(const UUID& other) noexcept; // copy assign |
48 | | |
49 | | bool valid() const noexcept; |
50 | | |
51 | | bool operator==(const UUID& other) const noexcept; |
52 | | bool operator!=(const UUID& other) const noexcept; |
53 | | bool operator<(const UUID& other) const noexcept; |
54 | | bool operator<=(const UUID& other) const noexcept; |
55 | | bool operator>(const UUID& other) const noexcept; |
56 | | bool operator>=(const UUID& other) const noexcept; |
57 | | // std::string operator=(const UUID& from) const noexcept; // TODO verify this syntax/location |
58 | | |
59 | | std::array<value_type, 16> data() const noexcept; |
60 | | std::string string() const noexcept; |
61 | | |
62 | | private: |
63 | | std::array<value_type, 16> mData = { {0}}; |
64 | | bool mValid; |
65 | | |
66 | | UUID(std::array<value_type, 16> data, bool isValid) noexcept; |
67 | | }; |
68 | | |
69 | | |
70 | | |
71 | | } // end namespace |
72 | | } // end namespace |
73 | | |
74 | | #endif |